//
//  GameWorld.cpp
//  IsoCplus
//
//  Created by zhouzhanglin on 15/9/17.
//
//

#include "GameWorld.h"
#include "Building.h"

USING_NS_CC;

void GameWorld::onEnter()
{
    BaseWorld::onEnter();
    
    groundScene = IsoScene::create(GRID_SIZE, GRID_X, GRID_Z);
    buildingScene = IsoScene::create(GRID_SIZE, GRID_X, GRID_Z);
    
    addScene(groundScene);
    addScene(buildingScene);
}

void GameWorld::onClick(const Vec2 &gridPos)
{
    if(_gridData->checkInGrid(gridPos.x,gridPos.y)){
        if(_gridData->getNode(gridPos.x,gridPos.y)->walkable){
           
            auto building = createCocObject();
            building->setNodePosition(gridPos.x,gridPos.y);
            if(building->getWalkable(_gridData)){
                building->setWalkable(false,_gridData);
                buildingScene->addIsoObject(building,true);
            }
            else
            {
                building = nullptr;
            }
        }
    }
}

IsoObject* GameWorld::createCocObject()
{
    auto temp = rand_0_1()>0.5 ? 1: 2;
    auto building = Building::create(GRID_SIZE,temp,temp);
    return building;
}